set(HCCL_CODE_ROOT ${TOP_DIR}/ace/comop/hccl/whole)
set(HCCL_OPEN_CODE_ROOT ${TOP_DIR}/ace/comop/hccl/open_source)
set(HCCL_OPEN_CODE_ALGORITHM ${HCCL_OPEN_CODE_ROOT}/src/domain/collective_communication/algorithm)
set(HCCL_OPEN_CODE_FRAMEWORK ${HCCL_OPEN_CODE_ROOT}/src/domain/collective_communication/framework)
set(HCCL_OPEN_CODE_COMMON ${HCCL_OPEN_CODE_ROOT}/src/domain/collective_communication/common)
set(HCCL_RESEARCH_CODE_ALGORITHM ${HCCL_CODE_ROOT}/hccl/algorithm/alg_research)
set(HCCL_ANALYZER_LIB_ROOT ${TOP_DIR}/llt/ace/comop/hccl/alg_analyzer)
set(HCCL_INSINGHT_LIB_ROOT ${TOP_DIR}/llt/ace/comop/hccl/insight)

set(include_list
    # 测试框架的代码
    ${HCCL_ANALYZER_LIB_ROOT}/inc
    ${HCCL_ANALYZER_LIB_ROOT}/checker
    ${HCCL_ANALYZER_LIB_ROOT}/stub
    ${HCCL_ANALYZER_LIB_ROOT}/stub/transport
    ${HCCL_ANALYZER_LIB_ROOT}/checker/rank_info_recorder
    ${HCCL_ANALYZER_LIB_ROOT}/checker/semantics_check
    ${HCCL_ANALYZER_LIB_ROOT}/checker/common/types
    ${HCCL_ANALYZER_LIB_ROOT}/checker/task_collector
    ${HCCL_ANALYZER_LIB_ROOT}/stub/notify
    ${HCCL_ANALYZER_LIB_ROOT}/stub/stream
    ${HCCL_ANALYZER_LIB_ROOT}/checker/link_type_recorder

    # studio工具的代码
    ${HCCL_INSINGHT_LIB_ROOT}/cpp_engine/src

    # hccl inc目录
    ${TOP_DIR}/ace/comop/inc
    ${TOP_DIR}/ace/comop/inc/hccl
    ${TOP_DIR}/ace/comop/inc/external
    ${TOP_DIR}/ace/comop/inc/external/hccl
    ${TOP_DIR}/ace/comop/hccl/whole/hccl/platform/inc

    # inc目录下头文件
    ${TOP_DIR}/metadef/inc/common/util/error_manager
    ${TOP_DIR}/ace/npuruntime/inc
    ${TOP_DIR}/abl/msprof/inc
    ${TOP_DIR}/abl/mmpa/inc/mmpa

    # 添加hccl目录
    ${HCCL_CODE_ROOT}/hccl/algorithm/pub_inc
    ${HCCL_CODE_ROOT}/hccl/algorithm/alg_research/pub_inc
    ${HCCL_CODE_ROOT}/hccl/algorithm/alg_research/aiv
    ${HCCL_CODE_ROOT}/hccl/pub_inc

    ${HCCL_OPEN_CODE_ALGORITHM}/base/inc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/task
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/registry
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/registry
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/resource_manager
    ${HCCL_OPEN_CODE_ALGORITHM}/base/alg_template
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator
    ${HCCL_OPEN_CODE_ALGORITHM}/base/mc2_handler
    ${HCCL_OPEN_CODE_ALGORITHM}/base/alg_template/component/inc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_reduce
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_reduce/310P
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_gather
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_gather/310P
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_gather_v
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter/310P
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter_v
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter_v/310P
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_broadcast
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_broadcast/310P
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_send_receive
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_to_all

    ${HCCL_OPEN_CODE_ALGORITHM}/impl/inc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/legacy
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/legacy/operator
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/legacy

    ${HCCL_RESEARCH_CODE_ALGORITHM}/aiv
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_scatter
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_reduce_scatter
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_reduce
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_broadcast
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_alltoall
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_alltoallv
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_all_reduce
    ${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_all_gather

    # framework目录下的头文件
    ${HCCL_OPEN_CODE_FRAMEWORK}/inc
    ${HCCL_OPEN_CODE_FRAMEWORK}/common/src/config
    ${HCCL_OPEN_CODE_FRAMEWORK}/common/src/topo
    ${HCCL_OPEN_CODE_FRAMEWORK}/communicator/impl

    # common目录下的头文件
    ${HCCL_OPEN_CODE_COMMON}/health/
    ${HCCL_OPEN_CODE_COMMON}/debug/profiling
    ${HCCL_OPEN_CODE_COMMON}/debug/profiling/inc

    # slog头文件
    ${TOP_DIR}/abl/slog/inc/toolchain
    ${TOP_DIR}/abl/atrace/inc/utrace
    ${HCCL_OPEN_CODE_ROOT}/test/algorithm/testcase
)


file(GLOB_RECURSE ALL_REDUCE_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_reduce/*.cc")
file(GLOB_RECURSE ALL_REDUCE_EXECUTOR_310P "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_reduce/310P/*.cc")

file(GLOB_RECURSE ALL_GATHER_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_gather/*.cc")
file(GLOB_RECURSE ALL_GATHER_EXECUTOR_310P "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_gather/310P/*.cc")

file(GLOB_RECURSE ALL_GATHER_V_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_gather_v/*.cc")

file(GLOB_RECURSE REDUCE_SCATTER_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter/*.cc")
file(GLOB_RECURSE REDUCE_SCATTER_EXECUTOR_310P "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter/310P/*.cc")

file(GLOB_RECURSE REDUCE_SCATTER_V_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter_v/*.cc")
file(GLOB_RECURSE REDUCE_SCATTER_EXECUTOR_310P "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce_scatter_v/310P/*.cc")

file(GLOB_RECURSE REDUCE_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_reduce/*.cc")

file(GLOB_RECURSE SEND_RECV_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_send_receive/*.cc")

file(GLOB_RECURSE BROADCAST_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_broadcast/*.cc")
file(GLOB_RECURSE BROADCAST_EXECUTOR_310P "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_broadcast/310P/*.cc")

file(GLOB_RECURSE ALL_TO_ALL_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_all_to_all/*.cc")

file(GLOB_RECURSE SCATTER_EXECUTOR "${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_scatter/*.cc")

file(GLOB_RECURSE TEMPLATE_BASE "${HCCL_OPEN_CODE_ALGORITHM}/base/alg_template/*.cc")
file(GLOB_RECURSE TEMPLATE_BASE_COMPONENT "${HCCL_OPEN_CODE_ALGORITHM}/base/alg_template/component/*.cc")

file(GLOB_RECURSE TEMP_SCATTER "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_scatter/*.cc")
file(GLOB_RECURSE TEMP_REDUCE_SCATTER "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_reduce_scatter/*.cc")
file(GLOB_RECURSE TEMP_REDUCE "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_reduce/*.cc")
file(GLOB_RECURSE TEMP_BROADCAST "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_broadcast/*.cc")
file(GLOB_RECURSE TEMP_ALLTOALL "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_alltoall/*.cc")
file(GLOB_RECURSE TEMP_ALLTOALLV "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_alltoallv/*.cc")
file(GLOB_RECURSE TEMP_ALL_REDUCE "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_all_reduce/*.cc")
file(GLOB_RECURSE TEMP_ALL_GATHER "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/temp_all_gather/*.cc")
file(GLOB_RECURSE TEMPLATE_RESEARCH "${HCCL_RESEARCH_CODE_ALGORITHM}/alg_template/*.cc")

set(src_to_test_list

)
add_executable(open_hccl_test ${src_to_test_list})

add_subdirectory(algorithm)
# 需要针对protobuf自动编译生成的文件设置GENERATED属性，否则会编译报错
set_property(SOURCE ${CMAKE_BINARY_DIR}/proto/alg_ayalyzer_proto/proto/analysis_result.pb.cc PROPERTY GENERATED 1)

#头文件搜索路径
target_include_directories(open_hccl_test PRIVATE
    ${include_list}
)

target_compile_definitions(open_hccl_test PRIVATE
    google=ascend_private
    -DOPEN_HCCL_TEST
)

target_compile_options(open_hccl_test PRIVATE
    -U_FORTIFY_SOURCE
    -Os
    -O0 -g
)

#本地是否有共享库存在
if(EXISTS "${HCCL_ANALYZER_LIB_ROOT}/../lib/libhccl_alg_analyzer.so" )
    find_library(LIBRARY_ANALYZER libhccl_alg_analyzer.so ${HCCL_ANALYZER_LIB_ROOT}/../lib)
    target_link_libraries(open_hccl_test PRIVATE
        ${LIBRARY_ANALYZER}
        hccl_alg_test
        hccl_alg_research_test
        $<BUILD_INTERFACE:intf_llt_pub>
        ascend_protobuf
        c_sec_static
        -lrt
        -ldl
    )
else()
    target_link_libraries(open_hccl_test PRIVATE
        hccl_alg_analyzer
        hccl_alg_test
        hccl_alg_research_test
        $<BUILD_INTERFACE:intf_llt_pub>
        ascend_protobuf
        c_sec_static
        -lrt
        -ldl
    )

add_dependencies(open_hccl_test insight_adapter_lib hccl_alg_analyzer_test hccl_alg_analyzer_lib hccl_alg_test_lib hccl_alg_research_test_lib)

endif()

set(src_list_alg_research
    ${HCCL_RESEARCH_CODE_ALGORITHM}/communicator/calc_nb_transport_req.cc
    ${HCCL_RESEARCH_CODE_ALGORITHM}/communicator/calc_nhr_transport_req.cc
    ${HCCL_RESEARCH_CODE_ALGORITHM}/communicator/calc_nhr_v1_transport_req.cc
    ${HCCL_RESEARCH_CODE_ALGORITHM}/communicator/calc_ahc_broke_transport_req.cc
    ${HCCL_RESEARCH_CODE_ALGORITHM}/communicator/calc_ahc_transport_req_base.cc
    ${HCCL_RESEARCH_CODE_ALGORITHM}/communicator/calc_ahc_transport_req.cc
    ${HCCL_RESEARCH_CODE_ALGORITHM}/aiv/hccl_aiv.cc

    ${TEMPLATE_RESEARCH}
    ${TEMP_SCATTER}
    ${TEMP_REDUCE_SCATTER}
    ${TEMP_REDUCE}
    ${TEMP_BROADCAST}
    ${TEMP_ALLTOALL}
    ${TEMP_ALLTOALLV}
    ${TEMP_ALL_REDUCE}
    ${TEMP_ALL_GATHER}
)

add_library(hccl_alg_research_test SHARED ${src_list_alg_research})

target_include_directories(hccl_alg_research_test PRIVATE ${include_list})

target_compile_options(hccl_alg_research_test PRIVATE
    -Werror
    -fno-common
    -fno-strict-aliasing
    -pipe
    -std=c++14
    -Os
    -O0 -g
    -fstack-protector-all
)

target_link_libraries(hccl_alg_research_test PRIVATE
    $<BUILD_INTERFACE:intf_llt_pub>
    ascend_protobuf
    c_sec_static
    -lrt
    -ldl
)

add_custom_target(hccl_alg_research_test_lib
    DEPENDS hccl_alg_research_test
    COMMAND mkdir -p ${CMAKE_INSTALL_PREFIX}/hccl_lib
    COMMAND cp libhccl_alg_research_test.so ${CMAKE_INSTALL_PREFIX}/hccl_lib
    COMMAND cd ${CMAKE_INSTALL_PREFIX}/hccl_lib
)

set(src_list_alg
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/topo_matcher.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/alg_configurator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_alg_utils.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/hccl_alg.cc

    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/topo_info_extractor.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/calc_hd_transport_req.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/calc_mesh_transport_req.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/calc_p2p_transport_req.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/calc_partial_mesh_transport_req.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/calc_ring_transport_req.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/base/communicator/calc_transport_req_base.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/base/mc2_handler/mc2_handler.cc

    ${HCCL_OPEN_CODE_ALGORITHM}/impl/task/threadManage.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/task/parallel_task_loader.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/task/task_loader.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/registry/coll_alg_op_registry.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/coll_alg_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/all_reduce_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/all_gather_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/all_gather_v_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/reduce_scatter_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/reduce_scatter_v_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/send_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/receive_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/broadcast_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/reduce_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/alltoall_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/scatter_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/operator/batchsendrecv_operator.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/registry/coll_alg_exec_registry.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_executor_base.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_native_executor_base.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/coll_comm_executor.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/coll_executor/alg_profiling.cc
    ${HCCL_OPEN_CODE_ALGORITHM}/impl/alg_env_config.cc

    ${ALL_REDUCE_EXECUTOR}
    ${ALL_REDUCE_EXECUTOR_310P}

    ${ALL_GATHER_EXECUTOR}
    ${ALL_GATHER_EXECUTOR_310P}

    ${ALL_GATHER_V_EXECUTOR}

    ${REDUCE_SCATTER_EXECUTOR}
    ${REDUCE_SCATTER_EXECUTOR_310P}

    ${REDUCE_SCATTER_V_EXECUTOR}

    ${SEND_RECV_EXECUTOR}

    ${BROADCAST_EXECUTOR}
    ${BROADCAST_EXECUTOR_310P}

    ${REDUCE_EXECUTOR}

    ${SCATTER_EXECUTOR}

    ${ALL_TO_ALL_EXECUTOR}

    ${TEMPLATE_BASE}
    ${TEMPLATE_BASE_COMPONENT}
)

add_library(hccl_alg_test SHARED ${src_list_alg})

target_include_directories(hccl_alg_test PRIVATE ${include_list})

target_compile_options(hccl_alg_test PRIVATE
    -Werror
    -fno-common
    -fno-strict-aliasing
    -pipe
    -std=c++14
    -Os
    -O0 -g
    -fstack-protector-all
)

target_compile_definitions(hccl_alg_test PRIVATE
    google=ascend_private
    -DOPEN_HCCL_TEST
)

target_link_libraries(hccl_alg_test PRIVATE
    -Wl,--no-as-needed
    hccl_alg_research_test
    -Wl,--no-as-needed
    $<BUILD_INTERFACE:intf_llt_pub>
    $<BUILD_INTERFACE:slog_headers>
    c_sec
)

add_custom_target(hccl_alg_test_lib
    DEPENDS hccl_alg_test
    COMMAND mkdir -p ${CMAKE_INSTALL_PREFIX}/hccl_lib
    COMMAND cp libhccl_alg_test.so ${CMAKE_INSTALL_PREFIX}/hccl_lib
    COMMAND cd ${CMAKE_INSTALL_PREFIX}/hccl_lib
)